package com.sqstudio.control
{
	import com.sqstudio.common.Sys;
	import com.sqstudio.event.CalendarEvent;
	import com.sqstudio.model.Model;
	import com.sqstudio.view.StarRmbPanel;

	public class StarRmbController
	{
		private var _starRmbPanel:StarRmbPanel;
		public function StarRmbController()
		{
			this._starRmbPanel = new StarRmbPanel();
			Sys.STAGE.addChild(this._starRmbPanel);
			this._starRmbPanel.x = 20;
			this._starRmbPanel.y = 407.5;
			
			CalendarEvent.Dispatcher.addEventListener(CalendarEvent.UPDATE_RMB,parseRMB);
			CalendarEvent.Dispatcher.addEventListener(CalendarEvent.UPDATE_STAR,parseStar);
			
		}
		
		private function parseStar(e:CalendarEvent):void
		{
			var starObj:Object = Model.getInstance().star_obj;
			var starArr:Array = new Array();
			var starValueArr:Array = new Array();
			
			var valueNum:int=0;
			for each(var o3:Object in starObj ){
				valueNum += int(o3.value);
			}
			if(valueNum <=5){
				var nowDate:Date = new Date();
				var days:Number = nowDate.date%12;
				for each(var oo:Object in starObj ){
					if(oo.id == days+1){
						oo.value=95;
					}else{
						oo.value=oo.id*6;
						if(oo.value<20) oo.value =20;
					}
				}
			}
			
			for each(var o:Object in starObj ){
				var value:String ="★";
				for(var i:int=0;i<int(int(o.value)/10);i++){
					value +="★";
				}
				starArr.push(o.name+" : "+value);
				starValueArr.push(int(o.value));
			}
			starValueArr.sort(compare);
			function compare(a:int,b:int):int{
				if(int(a)>int(b)){
					return -1;
				}
				return 1;
			}
			var starStr:String="白羊座";
			for each(var o2:Object in starObj ){
				if(int(o2.value) == starValueArr[0]){
					starStr = String(o2.name);
				}
			}
			
			starValueArr.sort();
			
			
			
			this._starRmbPanel.updateStarLabel([starArr,starStr]);
			
		}
		
		private function parseRMB(e:CalendarEvent):void{
			var rmbObj:Object = Model.getInstance().huilv_obj;
			var rmbArr:Array = new Array();
			for each(var o1:Object in rmbObj ){
				rmbArr.push(o1.name+"="+o1.value);
			}
			this._starRmbPanel.updateRMBLabel(rmbArr);
			
		}
	}
}